/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.standard.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.basicdata.standard.entity.BasicCheckMainForm;
import com.jeeplus.modules.basicdata.standard.mapper.BasicCheckMainFormMapper;
import com.jeeplus.modules.basicdata.standard.entity.BasicdataCheck;
import com.jeeplus.modules.basicdata.standard.mapper.BasicdataCheckMapper;
import com.jeeplus.modules.basicdata.standard.entity.BasicDataGiveoil;
import com.jeeplus.modules.basicdata.standard.mapper.BasicDataGiveoilMapper;

/**
 * 主表Service
 * @author 王洋
 * @version 2020-09-17
 */
@Service
@Transactional(readOnly = true)
public class BasicCheckMainFormService extends CrudService<BasicCheckMainFormMapper, BasicCheckMainForm> {

	@Autowired
	private BasicdataCheckMapper basicdataCheckMapper;
	@Autowired
	private BasicDataGiveoilMapper basicDataGiveoilMapper;

	public BasicCheckMainForm get(String id) {
		BasicCheckMainForm basicCheckMainForm = super.get(id);
		basicCheckMainForm.setBasicdataCheckList(basicdataCheckMapper.findList(new BasicdataCheck(basicCheckMainForm)));
		basicCheckMainForm.setBasicDataGiveoilList(basicDataGiveoilMapper.findList(new BasicDataGiveoil(basicCheckMainForm)));
		return basicCheckMainForm;
	}

	public List<BasicCheckMainForm> findList(BasicCheckMainForm basicCheckMainForm) {
		return super.findList(basicCheckMainForm);
	}

	public Page<BasicCheckMainForm> findPage(Page<BasicCheckMainForm> page, BasicCheckMainForm basicCheckMainForm) {
		return super.findPage(page, basicCheckMainForm);
	}

	@Transactional(readOnly = false)
	public void save(BasicCheckMainForm basicCheckMainForm) {
		super.save(basicCheckMainForm);
		for (BasicdataCheck basicdataCheck : basicCheckMainForm.getBasicdataCheckList()){
			if (basicdataCheck.getId() == null){
				continue;
			}
			if (BasicdataCheck.DEL_FLAG_NORMAL.equals(basicdataCheck.getDelFlag())){
				if (StringUtils.isBlank(basicdataCheck.getId())){
					basicdataCheck.setBasicdataPart(basicCheckMainForm);
					basicdataCheck.preInsert();
					basicdataCheckMapper.insert(basicdataCheck);
				}else{
					basicdataCheck.preUpdate();
					basicdataCheckMapper.update(basicdataCheck);
				}
			}else{
				basicdataCheckMapper.delete(basicdataCheck);
			}
		}
		for (BasicDataGiveoil basicDataGiveoil : basicCheckMainForm.getBasicDataGiveoilList()){
			if (basicDataGiveoil.getId() == null){
				continue;
			}
			if (BasicDataGiveoil.DEL_FLAG_NORMAL.equals(basicDataGiveoil.getDelFlag())){
				if (StringUtils.isBlank(basicDataGiveoil.getId())){
					basicDataGiveoil.setName(basicCheckMainForm);
					basicDataGiveoil.preInsert();
					basicDataGiveoilMapper.insert(basicDataGiveoil);
				}else{
					basicDataGiveoil.preUpdate();
					basicDataGiveoilMapper.update(basicDataGiveoil);
				}
			}else{
				basicDataGiveoilMapper.delete(basicDataGiveoil);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(BasicCheckMainForm basicCheckMainForm) {
		super.delete(basicCheckMainForm);
		basicdataCheckMapper.delete(new BasicdataCheck(basicCheckMainForm));
		basicDataGiveoilMapper.delete(new BasicDataGiveoil(basicCheckMainForm));
	}

}